var SelectPositionTypeModal = {
  inited: false,
  maxSelected: 0,
  callback: $.noop,

  init: function() {
    if (SelectPositionTypeModal.inited) {
      $('#select_position_type_modal').modal({ show: true });
      return;
    }

    $('#select_position_type_modal').on('shown', function() {
      $('#select_position_type_modal_selected').empty();
      $('#select_position_type_modal_options :checkbox').removeAttr('checked');
      // 取得传入的职能类别参数并选中
      var selectedIds = $('#select_position_type_modal').data('selectedIds');
      if (selectedIds) {
        for (var i = 0; i < selectedIds.length; i++) {
          if (selectedIds[i]) {
            SelectPositionTypeModal._selectPositionType(selectedIds[i]);
          }
        }
      }
      SelectPositionTypeModal.maxSelected = $('#select_position_type_modal').data('maxSelected');
      SelectPositionTypeModal.callback = $('#select_position_type_modal').data('callback') || $.noop;
    }).modal({ show: true });

    $('.position_type_table .position_type, .level3_table .level3_td').mouseenter(function() {
      $(this).addClass('hl');
    }).mouseleave(function() {
      $(this).removeClass('hl');
    });
    $('.position_type_table .position_type').click(function(event) {
      var l2Id = $(this).attr('l2_id');
      $('.level3_table').hide();
      var modalBody = $('#select_position_type_modal .modal-body');
      var l2Table = $('#_l2' + l2Id).show();
      var mouseOffsetX = event.pageX - modalBody.offset().left;
      var mouseOffsetY = event.pageY - modalBody.offset().top;
      var top = Math.max(mouseOffsetY + l2Table.height() > modalBody.height() ? mouseOffsetY - l2Table.height() : mouseOffsetY, 0);
      var left = Math.max(mouseOffsetX + l2Table.width() > modalBody.width() ? mouseOffsetX - l2Table.width() : mouseOffsetX, 0);
      l2Table.css({ left: left + 'px', top: top + 'px' });
    });
    $('.level3_table').mouseleave(function() {
      $(this).hide();
    });

    $('.level3_table input').change(function() {
      var $this = $(this);
      var id = $this.val();
      if ($this.attr('checked')) { // 选中
        if (SelectPositionTypeModal._reachedMax(id)) {
          window.alert('对不起，您最多只能选择' + SelectPositionTypeModal.maxSelected + '项。');
          $(this).removeAttr('checked');
          return;
        }
        SelectPositionTypeModal._selectPositionType(id);
      } else { // 取消选中
        SelectPositionTypeModal._removeSelectedPositionType(id);
      }
    });

    $('#select_position_type_modal_selected input').live('change', function() {
      SelectPositionTypeModal._removeSelectedPositionType($(this).val());
    });

    $('#select_position_type_modal_ok').on('click', function() {
      $('#select_position_type_modal').modal('hide');
      SelectPositionTypeModal.callback(SelectPositionTypeModal.getSelectedPositionTypes());
    });

    $('#select_position_type_modal_close').on('click', function() {
      $('#select_position_type_modal').modal('hide');
    });

    SelectPositionTypeModal.inited = true;
  },

  getSelectedPositionTypes: function() {
    var selected = [];
    $('#select_position_type_modal_selected input').each(function() {
      selected.push({ id: $(this).val(), name: $.trim($(this).parent().text()) });
    });
    return selected;
  },

  _reachedMax: function(id) {
    var selected = $('#select_position_type_modal_selected input').length;
    if (id.length === 4) {
      return selected - $('#_l2' + id + ' input:not(:first)[checked]').length + 1 > SelectPositionTypeModal.maxSelected;
    } else {
      return selected === SelectPositionTypeModal.maxSelected;
    }
  },

  _selectPositionType: function(id) {
    var l2id = id.length === 6 ? Math.floor(parseInt(id) / 100) : id;
    var selected = $('#select_position_type_modal_selected');
    var condition = id.length === 6 ? 'input[value="' + l2id + '"]' : 'input[value^="' + l2id + '"]';
    selected.find(condition).each(function() {
      SelectPositionTypeModal._removeSelectedPositionType($(this).val());
    });
    var option = $('#_l2' + l2id + ' input[value="' + id + '"]').attr('checked', '');
    option.parent().clone().appendTo(selected);
  },

  _removeSelectedPositionType: function(id) {
    var l2id = id.length ? Math.floor(parseInt(id) / 100) : id;
    $('#select_position_type_modal_selected :input[value="' + id + '"]').parent().remove();
    $('#_l2' + l2id + ' input[value="' + id + '"]').removeAttr('checked');
  }
};
